package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.system.domain.SysNotice;
import com.xmtlxx.system.domain.vo.NoticeUserVo;
import com.xmtlxx.system.domain.vo.SysNoticeVo;
import com.xmtlxx.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 公告 信息操作处理
 *
 * @author xmtlxx
 */
@Validated
@Api(value = "公告信息控制器", tags = {"公告信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {

    private final ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @ApiOperation("获取通知公告列表")
    @SaCheckPermission("datas:query:notice")
    @GetMapping("/list")
    public TableDataInfo<SysNotice> list(SysNotice notice, PageQuery pageQuery) {
        return noticeService.selectPageNoticeList(notice, pageQuery);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @ApiOperation("根据通知公告编号获取详细信息")
    @SaCheckPermission("datas:query:notice")
    @GetMapping(value = "/{noticeId}")
    public R<SysNoticeVo> getInfo(@ApiParam("公告ID") @PathVariable Long noticeId) {
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId);
        SysNoticeVo vo = BeanUtil.toBean(sysNotice,SysNoticeVo.class);
        if(StringUtils.isNotEmpty(vo.getPostIds())){
           String[] pids = vo.getPostIds().split(",");
           Long[] posts = new Long[pids.length];
           for(int i = 0;i<pids.length;i++){
               posts[i] = Long.valueOf(pids[i]);
           }
           vo.setPosts(posts);
        }
        if(StringUtils.isNotEmpty(vo.getUserIds())){
            String[] uids = vo.getUserIds().split(",");
            Long[] users = new Long[uids.length];
            for(int i = 0;i<uids.length;i++){
                users[i] = Long.valueOf(uids[i]);
            }
            vo.setUsers(users);
        }
        return R.ok(vo);
    }

    /**
     * 新增通知公告
     */
    @ApiOperation("新增通知公告")
    @SaCheckPermission("system:notice:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysNoticeVo vo) {

        SysNotice notice = BeanUtil.toBean(vo,SysNotice.class);
//        BeanUtil.copyProperties(vo,notice);
        notice.setTenantId(LoginHelper.getTenantId());
        if(vo.getPosts() != null && vo.getPosts().length > 0){
            String posts = "";
            for(Long post : vo.getPosts()){posts+=post+",";}
            posts = posts.substring(0,posts.length() -1);
            notice.setPostIds(posts);
        }
        if(vo.getUsers() != null && vo.getUsers().length > 0){
            String users = "";
            for(Long user : vo.getUsers()){users+=user+",";}
            users = users.substring(0,users.length() -1);
            notice.setUserIds(users);
        }
        return toAjax(noticeService.insertNotice(notice,vo.getPosts(),vo.getUsers()));
    }

    /**
     * 修改通知公告
     */
    @ApiOperation("修改通知公告")
    @SaCheckPermission("system:notice:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody SysNoticeVo vo) {
        SysNotice notice = BeanUtil.toBean(vo,SysNotice.class);
//        BeanUtil.copyProperties(vo,notice);
        if(vo.getPosts() != null && vo.getPosts().length > 0){
            String posts = "";
            for(Long post : vo.getPosts()){posts+=post+",";}
            posts = posts.substring(0,posts.length() -1);
            notice.setPostIds(posts);
        }
        if(vo.getUsers() != null && vo.getUsers().length > 0){
            String users = "";
            for(Long user : vo.getUsers()){users+=user+",";}
            users = users.substring(0,users.length() -1);
            notice.setUserIds(users);
        }
        return toAjax(noticeService.updateNotice(notice,vo.getPosts(),vo.getUsers()));
    }

    /**
     * 删除通知公告
     */
    @ApiOperation("删除通知公告")
    @SaCheckPermission("system:notice:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public R<Void> remove(@ApiParam("公告ID串") @PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    /**
     * 查询公告用户列表
     */
    @ApiOperation("查询公告用户列表")
    @SaCheckPermission("datas:query:notice")
    @GetMapping("/usernotices")
    public TableDataInfo<NoticeUserVo> usernotices(@Validated(QueryGroup.class) NoticeUserVo vo, PageQuery pageQuery) {
        return noticeService.selectPageNoticeUserList(vo,pageQuery);
    }
}
